package com.team2.hospital.controller;

import com.team2.hospital.entity.Doctor;
import com.team2.hospital.service.DoctorService;
import com.team2.hospital.service.OrderService;
import com.team2.hospital.service.PatientService;
import com.team2.hospital.utils.JwtUtil;
import com.team2.hospital.utils.ResponseData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private PatientService patientService;

    /**
     * 登录数据验证
     *
     * @param dId       医生ID
     * @param dPassword 医生密码
     * @return 返回登录结果，包含医生信息和token
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData login(@RequestParam(value = "dId") int dId, @RequestParam(value = "dPassword") String dPassword) {
        System.out.println("[login] 请求开始，时间：" + System.currentTimeMillis() + "，dId：" + dId);
        // 调用 doctorService 中的 login 方法验证医生信息
        Doctor doctor = this.doctorService.login(dId, dPassword);
        if (doctor != null) {
            Map<String, String> map = new HashMap<>();
            map.put("dName", doctor.getdName());
            map.put("dId", String.valueOf(doctor.getdId()));
            // 生成JWT token
            String token = JwtUtil.getToken(map);
            map.put("token", token);
            return ResponseData.success("登录成功", map);
        } else {
            return ResponseData.fail("登录失败，密码或账号错误");
        }
    }

    /**
     * 查看当天挂号列表
     *
     * @param dId    医生ID
     * @param oStart 挂号开始时间
     * @return 返回当天挂号信息
     */
    @RequestMapping("findOrderByNull")
    public ResponseData findOrderByNull(@Param(value = "dId") int dId, @RequestParam(value = "oStart") String oStart) {
        System.out.println("[findOrderByNull] 请求开始，时间：" + System.currentTimeMillis() + "，dId：" + dId + "，oStart：" + oStart);
        // 调用 orderService 查找当天的挂号列表
        return ResponseData.success("返回当天挂号信息成功", this.orderService.findOrderByNull(dId, oStart));
    }

    /**
     * 根据患者ID查询患者信息
     *
     * @param pId 患者ID
     * @return 返回患者信息
     */
    @RequestMapping("findPatientById")
    public ResponseData findPatientById(int pId) {
        System.out.println("[findPatientById] 请求开始，时间：" + System.currentTimeMillis() + "，pId：" + pId);
        // 调用 patientService 根据患者ID查询患者信息
        return ResponseData.success("返回患者信息成功！", this.patientService.findPatientById(pId));
    }

    /**
     * 分页根据科室查询所有医生信息
     *
     * @param pageNumber  当前页码
     * @param size        每页大小
     * @param query       查询条件
     * @param arrangeDate 排班日期
     * @param dSection    科室
     * @return 返回医生信息
     */
    @RequestMapping("findDoctorBySectionPage")
    public ResponseData findDoctorBySectionPage(int pageNumber, int size, String query, String arrangeDate, String dSection) {
        System.out.println("[findDoctorBySectionPage] 请求开始，时间：" + System.currentTimeMillis() + "，pageNumber：" + pageNumber + "，size：" + size + "，query：" + query + "，arrangeDate：" + arrangeDate + "，dSection：" + dSection);
        // 调用 doctorService 根据科室分页查询医生信息
        return ResponseData.success("分页根据科室查询所有医生信息成功", this.doctorService.findDoctorBySectionPage(pageNumber, size, query, arrangeDate, dSection));
    }

    /**
     * 用户评价医生
     *
     * @param dId   医生ID
     * @param dStar 评价星级
     * @return 返回评价结果
     */
    @RequestMapping("updateStar")
    public ResponseData updateStar(int dId, Double dStar) {
        System.out.println("[updateStar] 请求开始，时间：" + System.currentTimeMillis() + "，dId：" + dId + "，dStar：" + dStar);
        // 调用 doctorService 更新医生评价
        if (this.doctorService.updateStar(dId, dStar)) {
            return ResponseData.success("评价成功");
        }
        return ResponseData.fail("评价失败");
    }

    /**
     * 上传Excel导入数据
     *
     * @param multipartFile 上传的Excel文件
     * @return 返回上传结果
     * @throws Exception 异常
     */
    @RequestMapping(value = "uploadExcel", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData uploadExcel(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        System.out.println("[uploadExcel] 请求开始，时间：" + System.currentTimeMillis() + "，文件名：" + multipartFile.getOriginalFilename());
        // 调用 doctorService 上传并处理Excel数据
        if (this.doctorService.uploadExcel(multipartFile)) {
            return ResponseData.success("上传Excel导入数据成功");
        }
        return ResponseData.fail("上传Excel导入数据失败");
    }

    /**
     * Excel导出数据
     *
     * @param response HttpServletResponse 用于文件下载
     * @return 返回导出结果
     * @throws IOException 异常
     */
    @RequestMapping("downloadExcel")
    public ResponseData downloadExcel(HttpServletResponse response) throws IOException {
        System.out.println("[downloadExcel] 请求开始，时间：" + System.currentTimeMillis());
        // 调用 doctorService 导出Excel数据
        if (this.doctorService.downloadExcel(response)) {
            return ResponseData.success("Excel导出数据成功");
        }
        return ResponseData.fail("Excel导出数据失败");
    }

}
